package com.mall.business.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
        import com.mall.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mall.business.mapper.TMemberLevelMapper;
import com.mall.business.domain.TMemberLevel;
import com.mall.business.service.ITMemberLevelService;

/**
 * 会员等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-31
 */
@Service
public class TMemberLevelServiceImpl extends ServiceImpl<TMemberLevelMapper,TMemberLevel>  implements ITMemberLevelService
{
    @Autowired
    private TMemberLevelMapper tMemberLevelMapper;

    /**
     * 查询会员等级
     *
     * @param id 会员等级主键
     * @return 会员等级
     */
    @Override
    public TMemberLevel selectTMemberLevelById(String id)
    {
        return tMemberLevelMapper.selectTMemberLevelById(id);
    }

    /**
     * 查询会员等级列表
     *
     * @param tMemberLevel 会员等级
     * @return 会员等级
     */
    @Override
    public List<TMemberLevel> selectTMemberLevelList(TMemberLevel tMemberLevel)
    {
        return tMemberLevelMapper.selectTMemberLevelList(tMemberLevel);
    }

    /**
     * 新增会员等级
     *
     * @param tMemberLevel 会员等级
     * @return 结果
     */
    @Override
    public int insertTMemberLevel(TMemberLevel tMemberLevel)
    {
                tMemberLevel.setCreateTime(DateUtils.getNowDate());
            return tMemberLevelMapper.insertTMemberLevel(tMemberLevel);
    }

    /**
     * 修改会员等级
     *
     * @param tMemberLevel 会员等级
     * @return 结果
     */
    @Override
    public int updateTMemberLevel(TMemberLevel tMemberLevel)
    {
                tMemberLevel.setUpdateTime(DateUtils.getNowDate());
        return tMemberLevelMapper.updateTMemberLevel(tMemberLevel);
    }

    /**
     * 批量删除会员等级
     *
     * @param ids 需要删除的会员等级主键
     * @return 结果
     */
    @Override
    public int deleteTMemberLevelByIds(String[] ids)
    {
        return tMemberLevelMapper.deleteTMemberLevelByIds(ids);
    }

    /**
     * 删除会员等级信息
     *
     * @param id 会员等级主键
     * @return 结果
     */
    @Override
    public int deleteTMemberLevelById(String id)
    {
        return tMemberLevelMapper.deleteTMemberLevelById(id);
    }




    /**
    *
    * 根据用户金额 确定会员等级
    *
    *
    */
    @Override
    public int getMemberLevel(BigDecimal   totalMoney)
    {
        Integer  level = 1;
        QueryWrapper<TMemberLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("level");
        List<TMemberLevel> list = this.list(queryWrapper);
        for (int i = 0; i <list.size() ; i++) {
            TMemberLevel tMemberLevel = list.get(i);
            BigDecimal minMoney = tMemberLevel.getMinMoney();
            BigDecimal maxMoney = tMemberLevel.getMaxMoney();
            if(totalMoney.compareTo(minMoney)>=0 && totalMoney.compareTo(maxMoney)<=0){
                level= tMemberLevel.getLevel().intValue();
                break;
            }
        }
        return   level;
    }


    @Override
    public TMemberLevel getUserMemberLevel(BigDecimal   totalMoney)
    {
        TMemberLevel level = null;
        QueryWrapper<TMemberLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("level");
        List<TMemberLevel> list = this.list(queryWrapper);
        for (int i = 0; i <list.size() ; i++) {
            TMemberLevel tMemberLevel = list.get(i);
            BigDecimal minMoney = tMemberLevel.getMinMoney();
            BigDecimal maxMoney = tMemberLevel.getMaxMoney();
            if(totalMoney.compareTo(minMoney)>=0 && totalMoney.compareTo(maxMoney)<=0){
                level= tMemberLevel;
                break;
            }
        }
        return   level;
    }


    /**
    *
    * 获取折扣 根据等级
    *
    *
    */
    @Override
    public BigDecimal getMemberRebateByLevel(Integer level)
    {
        QueryWrapper<TMemberLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level",level);
        queryWrapper.orderByAsc("level");
        List<TMemberLevel> list = this.list(queryWrapper);
        if(list.size()>0){
            TMemberLevel tMemberLevel = list.get(0);
            BigDecimal rebate = tMemberLevel.getRebate();
            return  rebate;
        }else{
            return  null;
        }
    }





}
